﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace GenerateMarkdown
{
    class Http
    {
        public static string GitHubPut(string token,string userID,string url, string content)
        {
            try
            {
                using (var client = new WebClient())
                {
                    //client.Encoding = Encoding.UTF8;
                    //client.Headers.Add("Accept", "application/json"); // 设置响应数据的ContentType
                    //client.Headers.Add("Content-Type", "application/json"); // 设置请求数据的ContentType
                    client.Headers.Add("User-Agent", userID);
                    //client.Headers.Add("ContentLength", content.Length.ToString());
                    client.Headers.Add("Authorization", "token "+ token);
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    var response = client.UploadString(url, "PUT", content); //content 是data
                    return response;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GiteePost(string url, string content)
        {
            try
            {
                using (var client = new WebClient())
                {
                    //client.Encoding = Encoding.UTF8;
                    //client.Headers.Add("Accept", "application/json"); // 设置响应数据的ContentType
                    client.Headers.Add("Content-Type", "application/json"); // 设置请求数据的ContentType
                    client.Headers.Add("charset", "UTF-8");
                    //client.Headers.Add("ContentLength", content.Length.ToString());
                    //client.Headers.Add("Authorization", "token " + token);
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    var response = client.UploadString(url, "POST", content); //content 是data
                    return response;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string Get(string url, string content)
        {
            try
            {
                using (var client = new WebClient())
                {
                    client.Encoding = Encoding.UTF8;
                    client.Headers.Add("Accept", "application/json"); // 设置响应数据的ContentType
                    return client.DownloadString(url + content);
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
